#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
	char mssv[10];
	char hoten[40];
	int tuoi;
	float toan,ly,hoa;
}sinhvien;
int main(){
	FILE * file;
	FILE * in;
	file = fopen("output.txt","w");
	if(file != NULL){
		printf("Mo File thanh cong\n Bam phim bat ky de tiep tuc.");
		getch();
	}else{
		printf("Mo File khong thanh cong\n Bam phim bat ky de ket thuc.");
		getch();
		exit(0);
	}
	sinhvien sv[10];
	int sosv;
	printf("\nnhap so sinh vien: ");
	scanf("%d",&sosv);
	printf("\n=================NHAP THONG TIN SINH VIEN===================\n");
	for(int i=0; i<sosv; i++){
		printf("\n=====sinh vien thu %d===== \n",i+1);
		printf("nhap mssv: ");
		scanf("%s",&sv[i].mssv);
		fflush(stdin);
		printf("nhap ho va ten: ");
		gets(sv[i].hoten);
		fflush(stdin);
		lbnhaptuoi:
			printf("nhap tuoi: ");
		    scanf("%d",&sv[i].tuoi);
		    if(sv[i].tuoi<0 || sv[i].tuoi>100){
		    	printf("\ntuoi nhap khong hop le moi nhap lai!");
		        goto lbnhaptuoi;
			}
		printf("nhap diem toan : ");
		scanf("%f",&sv[i].toan);
		printf("nhap diem ly: ");
		scanf("%f",&sv[i].ly);
		printf("nhap diem hoa: ");
		scanf("%f",&sv[i].hoa);
	}
	printf("\n=================THONG TIN SINH VIEN DA NHAP (Screen) ==================\n");
	for(int i=0; i<sosv ; i++){
		printf("\nsinh vien thu %d: \n",i+1);
		printf("\n\tmssv:      %s",sv[i].mssv);
		printf("\n\tho va ten: %s",sv[i].hoten);
	    printf("\n\ttuoi:      %d",sv[i].tuoi);
	    printf("\n\tdiem toan: %f",sv[i].toan);
	    printf("\n\tdiem ly:   %f",sv[i].ly);
	    printf("\n\tdiem hoa:  %f",sv[i].hoa);
	    printf("\n");
	}
	fprintf(file,"%s","\n=================THONG TIN SINH VIEN DA NHAP (file)==================\n");
	for(int i=0; i<sosv ; i++){
		fprintf(file,"%s","\nsinh vien thu ");
		fprintf(file,"%d",i+1);
		fprintf(file,"%s","\n\tmssv:   ");
		fprintf(file,"%s",sv[i].mssv);
	}
	printf("\n");
	printf("\n=================TINH DIEM TRUNG BINH====================\n");
	float dtb[10];
	for(int i=0; i<sosv; i++){
		printf("\ndiem trung binh cua sinh vien thu %d: ",i+1);
		dtb[i] = (sv[i].toan + sv[i].ly + sv[i].hoa )/3;
		printf("%0.2f",dtb[i]);
	}
	printf("\n");
	printf("\n===============DANH SACH SINH VIEN CO DTB TREN 5.0================\n");
	printf("\ndanh sach sinh vien co diem trung binh >5.0: ");
	for(int i=0; i<sosv; i++){
		if(dtb[i] > 5.0){
			printf("\t%s",sv[i].hoten);
		}
	}
	fclose(file);
	getch();
}
